<?php

Class _Goods extends Connect{
    
    var $table = "goods"; 
    public static $goods = array();   
    
    function __construct(){
        #_echo("__construct");
        if( !is_array($_SESSION['_goods']) ){
            _echo("_goods = arr");
            $_SESSION['_goods'] = array();
        }
    }
    
    function add($user, $id, $type, $present = false, $count = 1){        
        $use = 0;
        /*if($type == "mortar" ||
           $type == "boiler" ||
           $type == "podkova" ||
           $type == "skin" ){
           $use = 1; 
        }*/
        Goods::getAll();
        $sql = "INSERT INTO `".PREFIX_TABLE.$this->table."` 
            (`user`, `parent`, `type`, `use`) VALUES ";
        for($i = 1; $i <= $count; $i++ ){
            $sql .= "('".$user."', '".$id."', '".$type."', '".$use."')";
            $sql .= ($i < $count)? " , " : "" ;            
        }
        #return $sql; 
        if(!$result = DBConnect::query($sql)){
            #return $glade;
            $this->_error(mysql_error());
            return false;
        }
        if( !$present ){
            _Goods::$goods[$type][mysql_insert_id()]['id'] = mysql_insert_id();
            _Goods::$goods[$type][mysql_insert_id()]['parent'] = $id;
            _Goods::$goods[$type][mysql_insert_id()]['user'] = $user;
            _Goods::$goods[$type][mysql_insert_id()]['type'] = $type;
            _Goods::$goods[$type][mysql_insert_id()]['use'] = $type;    
        }
        return true; 
    }
    function addArtifact($user, $id){
        return $this->add($user, $id, "artifact");
    }
    function addClothing($user, $id){
        return $this->add($user, $id, "clothing");
    }
    function addBoiler($user, $id){
        return $this->add($user, $id, "boiler");
    }
    
    function _isit($user, $id, $type, $count = 1){
        
        $this->getMy($user);
        #return $_SESSION['_goods'][$type][$id];
        #return _Goods::$goods[$type];
        $c = 0;
        if(is_array(_Goods::$goods[$type]) && count(_Goods::$goods[$type]) > 0) foreach(_Goods::$goods[$type] as $k => $v){
            if($v['parent'] == $id){
                $c++;
                if($c >= $count){
                    return $v;    
                }                
            }
        }
        
        $sql = "SELECT * FROM `".PREFIX_TABLE.$this->table."` 
        WHERE `user` = '".$user."' AND `parent` = '".$id."'  AND `type` = '".$type."'";
        if(!$result = DBConnect::query($sql)){
            #return $glade;
            $this->_error(mysql_error());
            return false;
        }
        $arr = DBConnect::getOne($result);
        #if(!isset($arr['id'])) return false;
        if(DBConnect::mysqlCount($result) >= $count) return true;
        return false;
    }
    
    function _del($user, $id, $type  ){        
        $sql = "DELETE FROM `".PREFIX_TABLE.$this->table."` 
        WHERE `id` = '".$id."' AND `user` = '".$user."' LIMIT 1";
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        if(is_array(_Goods::$goods[$type]) && count(_Goods::$goods[$type]) > 0) foreach(_Goods::$goods[$type] as $key => $val){
                if($val['id'] == $id){
                    #unset($_SESSION['_goods'][$type][$key]);
                    unset(_Goods::$goods[$type][$key]);
                    return true;
                }    
            }
        return true;        
    }
    
    function del($user, $id, $type, $use = 0, $count = 1 ){        
        $sql = "DELETE FROM `".PREFIX_TABLE.$this->table."` 
        WHERE `parent` = '".$id."' AND `user` = '".$user."' AND `use` = '".$use."' AND `type` = '".$type."' LIMIT ".intval($count);
        if(!$result = DBConnect::query($sql)){
            #return $glade;
            $this->_error(mysql_error());
            return false;
        }
        #if(is_array($_SESSION['_goods'][$type]) && count($_SESSION['_goods'][$type]) > 0) foreach($_SESSION['_goods'][$type] as $key => $val){
        if(is_array(_Goods::$goods[$type]) && count(_Goods::$goods[$type]) > 0) foreach(_Goods::$goods[$type] as $key => $val){
                if($val['parent'] == $id){
                    unset($_SESSION['_goods'][$type][$key]);
                    unset(_Goods::$goods[$type][$key]);
                    return true;
                }    
            }
        return true;        
    }
    
    function getMy($user, $id = false, $type = false){        
        #return count($this->all);
        #_echo($_SESSION['_goods']);
        /*if(is_array($_SESSION['_goods']) && count($_SESSION['_goods']) > 0 ){
            _echo("count ".count($_SESSION['_goods']));
            return true;    
        }*/
        if(is_array(_Goods::$goods) && count(_Goods::$goods) > 0 ){
            return true;    
        }
           
        #_echo("count0"); 
        /*if($type){
            _echo("count goods ".$type." = ".count($_SESSION['_goods'][$type]));
            if(count($_SESSION['_goods'][$type]) > 0){
                return true;    
            }
        }else{
            _echo("count goods = ".count($_SESSION['_goods']));
            if(count($_SESSION['_goods']) > 0){
                return true;    
            }    
        }*/
        $sql = "SELECT * FROM `".PREFIX_TABLE.$this->table."` 
        WHERE `user` = '".$user."' ";
        _echo($sql);
        /*if($id){
            $sql .= " AND `parent` = '".$id."' ";
        }
        if($type){
            $sql .= " AND `type` = '".$type."' ";
        } */ 
        #return $sql;      
        if(!$result = DBConnect::query($sql)){
            #return $glade;
            $this->_error(mysql_error());
            return false;
        }
        #return
        $_arr = array();
        while($arr = DBConnect::getOne($result)){
            #_echo($arr["type"]);
            _Goods::$goods[$arr["type"]][$arr["id"]] = $arr; 
            #$_SESSION['_goods'][$arr["type"]][$arr["id"]] = $arr;   
        } 
        return true;
    }
    
    function _use($user, $id, $type, $idM = 0){
        
        if($tupe == 'food'){
            $sql = "DELETE FROM `".PREFIX_TABLE.$this->table."`  
                    WHERE `user` = '".$user."' AND `parent` = '".$id."' AND `type` = '".$type."'
                    LIMIT 1  ";
            if(!$result = DBConnect::query($sql)){
                $this->_error(mysql_error());
                return false;
            }
            #if(is_array($_SESSION['_goods'][$type]) && count($_SESSION['_goods'][$type]) > 0) foreach($_SESSION['_goods'][$type] as $key => $val){
            if(is_array(_Goods::$goods[$type]) && count(_Goods::$goods[$type]) > 0) foreach(_Goods::$goods[$type] as $key => $val){
                if($val['parent'] == $id){
                    unset($_SESSION['_goods'][$type][$key]);
                    unset(_Goods::$goods[$type][$key]);
                    return true;
                }    
            }
            return true;
        }
        
        $sql = "UPDATE `".PREFIX_TABLE.$this->table."` 
                SET
                `use` = '1',
                `idM` = '".$idM."' 
                WHERE `use` = '0' AND `user` = '".$user."' AND `type` = '".$type."'
            AND `parent` = '".$id."'
            LIMIT 1  ";
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        #if(is_array($_SESSION['_goods'][$type]) && count($_SESSION['_goods'][$type]) > 0) foreach($_SESSION['_goods'][$type] as $key => $val){
        if(is_array(_Goods::$goods[$type]) && count(_Goods::$goods[$type]) > 0) foreach(_Goods::$goods[$type] as $key => $val){
            if($val['parent'] == $id){
                #$_SESSION['_goods'][$type][$key]['use'] = 1;
                _Goods::$goods[$type][$key]['use'] = 1;
                return true;
            }    
        }
        return true;
    }
    
    function _unuse($user, $id, $type){
        $sql = "UPDATE `".PREFIX_TABLE.$this->table."` SET
            `use` = '0' WHERE `use` = '1' AND `user` = '".$user."' AND `type` = '".$type."'
            AND `parent` = '".$id."'
            LIMIT 1  ";
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        #if(is_array($_SESSION['_goods'][$type]) && count($_SESSION['_goods'][$type]) > 0) foreach($_SESSION['_goods'][$type] as $key => $val){
        if(is_array(_Goods::$goods[$type]) && count(_Goods::$goods[$type]) > 0) foreach(_Goods::$goods[$type] as $key => $val){
            if($val['parent'] == $id){
                #$_SESSION['_goods'][$type][$key]['use'] = 0;
                _Goods::$goods[$type][$key]['use'] = 0;
                return true;
            }    
        }
        return true;
    }
}
?>